//银行列表 0
//绑定银行卡 1


define(function(require, exports, module){
	"require:nomunge,exports:nomunge,module:nomunge";
	//异步请求
var wind_async=require('ddf_wind_async');
	
var base=require('component-base');
var history=require('component-history');
var dialog=require('component-dialog');
	
require('component-page');

/**
 * 查询银行卡列表
 */
exports.init=function(m){
	history.addHis(this,arguments);//增加历史  
    window.$ddf_bank = m;
	
		var htmlStr="";
		
		htmlStr += '<div id="content-header"><div id="breadcrumb"> <a href=javascript:load("'+ddf.index.index_js+'") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href="javascript:void(0)" class="current">银行帐号管理</a></div></div>';
		htmlStr += '<div class="container-fluid">';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';
		htmlStr += '<div id="alert-tips0" ></div>';
		
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title">';
		htmlStr += '<span class="icon"><i class="icon-pencil"></i></span>';
		htmlStr += '<h5>操作</h5>';
		htmlStr += '</div>  ';
		htmlStr += '<div class="widget-content">';
		htmlStr += '<div class="div-btn">';
		htmlStr += '<button class="btn btn-success" type="button" onclick="$ddf_bank.bindingbankcardPage()">绑定银行卡</button>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>银行卡列表</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding dataTables_wrapper">';
		htmlStr += '<table class="table table-bordered data-table">';
		htmlStr += '<thead>';
		htmlStr += '<tr>';
		htmlStr += '<th>序号</th>';
		htmlStr += '<th>银行名称</th>';
		htmlStr += '<th>账户姓名</th>';
		htmlStr += '<th>账户号</th>';
		htmlStr += '<th>绑定时间</th>';
		htmlStr += '<th>默认</th>';
		htmlStr += '<th>操作</th>';
		htmlStr += '</tr>';
		htmlStr += '</thead>';
		htmlStr += '<tbody id="quertBankList">';
		htmlStr += '</tbody>';
		htmlStr += '</table>';
		htmlStr += '<div  id="footPageDiv0">';

		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>  ';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '</div> ';

		showDiv("content0");

		$("#content0").html(htmlStr);
		
		getQueryBankList(1,pageSize);

		

}

/**
 * 查询会员列表
 */
function getQueryBankList(pageNumber,pageSize){
	

	base.showLoading();
	
	var params={
			
			"page.pageNumber":pageNumber,
			"page.pageSize":pageSize,
			
	        };
	
	eval(Wind.compile("async",function(){
		
		var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.bank.queryBank,params));
		
		getQueryBankListCallBack(data);
		
	}))().start();
	
}

/**
 * 查询会员列表_回调函数
 */
function getQueryBankListCallBack(data){
	
	if(data["code"]==1){
		

		var htmlStr = "";

		for (var i = 0; i < data["BankListData"].length; i++) {

			htmlStr += '<tr>';
			htmlStr += '<td>' +getSequence(data["pageNumber"],i+1) + '</td>';
			htmlStr += '<td>'+data["BankListData"][i]["bankName"]+'</td>';
			htmlStr += '<td>'+data["BankListData"][i]["accountName"]+'</td>';
			htmlStr += '<td>'+data["BankListData"][i]["bankNumber"]+'</td>';
			htmlStr += '<td>'+data["BankListData"][i]["createTime"]+'</td>';
			
			if(data["BankListData"][i]["defaultFlag"]==1){
				htmlStr += '<td>是</td>';
				htmlStr += '<td>&nbsp;</td>';
			}else{
				htmlStr += '<td>否</td>';
				htmlStr += '<td><button class="btn btn-success" onclick="$ddf_bank.setdefaultaccount(\''+data["BankListData"][i]["accountId"]+'\',\''+data["BankListData"][i]["bankNumber"]+'\')">设置为默认</button></td>';
			}
			
			htmlStr += '</tr>';

		}


		$("#quertBankList").html(htmlStr);

		 $('#footPageDiv0').ddf_pagination(data["pageSize"],data["pageNumber"],data["totalCount"],data["totalPage"],{  
			 
			 changePageCallback:function(pageSize,page){  
				 getQueryBankList(page,pageSize);

			 },
			 changePageSizeCallBack:function(pageSize){
				 getQueryBankList(1,pageSize);
	     	 
			 } 
	     }); 

	}else{
		
		base.tips_error(data["message"]);
	}
	
	base.cancelLoading();
	
}

///**
// *	银行卡解绑 
// */
//function unbindbanks(accountId,bankNumber) {
//	if (confirm("确定解除账户  "+bankNumber+" 的绑定记录?")) {
//
//		var params={
//				
//				"accountId":accountId
//				
//		        };
//		
//		base.showLoading();
//			
//		eval(Wind.compile("async",function(){
//			
//			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.bank.unbindbanksCard,params));
//			
//			unbindbanksCardCallBack(data);
//			
//		}))().start();
//	}
//}


///**
// * 解绑银行卡_回调函数
// */
//function unbindbanksCardCallBack(data){
//	base.cancelLoading();
//	if(data["code"]==1){
//		getQueryBankList(1,pageSize);
//		base.tips("银行卡解绑成功！");
//	}else{
//		 base.tips_error(data["message"]);
//	}
//	
//	
//}


///*
// * 重新绑定银行卡
// */
//function rebindbanks(accountId) {
//	if (accountId == "") {
//		base.tips(data["账户ID不能为空!"]);
//	} else {
//		
//		var params={
//				
//				"accountId":accountId
//				
//		        };
//		
//		base.showLoading();
//		
//		eval(Wind.compile("async",function(){
//			
//			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.bank.rebindbanksCard,params));
//			
//			rebindbanksCardCallBack(data);
//			
//		}))().start();
//	}
//	base.cancelLoading();
//
//}

///**
// * 重新绑定银行卡_回调函数
// */
//function rebindbanksCardCallBack(data){
//	base.cancelLoading();
//	if(data["code"]==1){
//		getQueryBankList(1);
//		base.tips("重新绑定银行卡成功！");
//	}else{
//		 base.tips_error(data["message"]);
//	}
//	
//	
//}


/**
 *	设为默认账户 
 */
function setdefaultaccount(accountId,bankNumber) {
	if (confirm("您只能有一个默认结算账户,确认要将结算账户变更为" + bankNumber + "吗?")) {
		var params={
				
				"sgAccount.accountId":accountId
				
		        };
		
		base.showLoading();
		
		eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.bank.setdefaultaccount,params));
			
			setdefaultaccountCallBack(data);
			
		}))().start();
	}
	
}

/**
 * 设为默认账户_回调函数
 */
function setdefaultaccountCallBack(data){
	base.cancelLoading();
	if(data["code"]==1){
		getQueryBankList(1,pageSize);
		base.tips("默认账户成功！");
	}else{
		 base.tips_error(data["message"]);
	}
	
	
}




/**
 * 打开绑定银行卡页面
 */
function bindingbankcardPage(){
	history.addHis(this,arguments);//增加历史
	
	var htmlStr="";
	
	htmlStr += '<div id="content-header"><div id="breadcrumb"> <a href=javascript:load("'+ddf.index.index_js+'") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href="javascript:$ddf_bank.showDiv("content0")>银行帐号管理</a><a href="javascript:void(0)" class="current">银行卡绑定</a></div></div>';
	htmlStr += '<div class="container-fluid">';
	htmlStr += '<div class="row-fluid">';
	htmlStr += '<div class="span12">';
	htmlStr += '<div id="alert-tips1" ></div>';
	
	htmlStr += '<div class="widget-box">';
	htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
	htmlStr += '<h5>绑定银行卡</h5>';
	htmlStr += '</div>';
	htmlStr += '<div class="widget-content nopadding">';
	htmlStr += '<form class="form-horizontal">	   ';
	
	htmlStr += '<div class="control-group" id="control-group-bankId" >';
	htmlStr += '<label class="control-label">银行卡开户行：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span4 alert_w"  id="input-bankCard" type="text" value="请选择银行卡开户行" readonly onclick="$ddf_bank.openBankCardDialog()" />';

	htmlStr += '</div>';
	htmlStr += '</div>';
	/*
	htmlStr += '<div class="control-group">';
	htmlStr += '<label class="control-label">账户类型：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<div class="btn-group"><button data-toggle="dropdown" class="btn dropdown-toggle" ><span id="account_type_name">银行卡</span>&nbsp;&nbsp;<span class="caret"></span></button><ul class="dropdown-menu">';
	htmlStr += '<li><a href=javascript:$ddf_bank.setAccountType("00","银行卡")>银行卡</a></li>';
	htmlStr += '<li><a href=javascript:$ddf_bank.setAccountType("01","银行存折")>银行存折</a></li>';
	htmlStr += '</ul></div> ';
	htmlStr += '</div>    ';
	htmlStr += '</div>';
	*/
	
	htmlStr += '<div class="control-group" id="control-group-bankNumber">';
	htmlStr += '<label class="control-label">银行卡号：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span4" type="text" id="input-bankNumber" value="">';
	htmlStr += '</div>              ';
	htmlStr += '</div>';

	htmlStr += '<div class="control-group" id="control-group-accountName">';
	htmlStr += '<label class="control-label">持卡人姓名：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span4" type="text" id="input-accountName" value="">';
	htmlStr += '</div>              ';
	htmlStr += '</div>';
	/*
	htmlStr += '<div class="control-group">';
	htmlStr += '<label class="control-label">证件类型：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<div class="btn-group">';
	htmlStr += '<button data-toggle="dropdown" class="btn dropdown-toggle" ><span id="card_type_name">身份证</span>&nbsp;&nbsp;<span class="caret"></span></button>';
	htmlStr += '<ul class="dropdown-menu">';
	htmlStr += '<li><a href=javascript:$ddf_bank.setCardType(1,"身份证")>身份证</a></li>';
	htmlStr += '<li><a href=javascript:$ddf_bank.setCardType(2,"军人证")>军人证</a></li>';
	htmlStr += '<li><a href=javascript:$ddf_bank.setCardType(3,"户口本")>户口本</a></li>';
	htmlStr += '</ul>';
	htmlStr += '</div>';
	htmlStr += '</div> ';
	htmlStr += '</div>';
	htmlStr += '<div class="control-group" id="control-group-identity">';
	htmlStr += '<label class="control-label">证件号码：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span4" type="text" id="input-identity" value="">';
	htmlStr += '</div>       ';
	htmlStr += '</div>';
	
	htmlStr += '<div class="control-group" id="control-group-mobile">';
	htmlStr += '<label class="control-label">手机号码：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span4" type="text" id="input-mobile">       ';
	htmlStr += '</div>                    ';
	htmlStr += '</div>';
	*/
	htmlStr += '<div class="control-group" >';
	htmlStr += '<label class="control-label"></label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input style="position: relative; top: 3px; z-index: 50;" id="defaultAcountCB" type="checkbox" checked="checked" value="true" onClick="$ddf_bank.setDefaultAcount()" />设为默认结算账户';
	htmlStr += '</div>             ';
	htmlStr += '</div>';
	/*
	htmlStr += '<div class="control-group" >';
	htmlStr += '<label class="control-label"></label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input style="position: relative; top: 3px; z-index: 50;"name="redisabledCB" id="redisabledCB" type="checkbox"onclick="$ddf_bank.redisabled()" onChange="$ddf_bank.redisabled()" /> 同意&nbsp;&nbsp;快捷支付服务协议&nbsp;&nbsp;，下次可凭卡号后4位快速付款';
	htmlStr += '</div>                  ';
	htmlStr += '</div>';
	*/
	
	htmlStr += '<div class="form-actions">';
	htmlStr += '<button class="btn btn-primary" id="btnBindBankCard" type="button" onclick="$ddf_bank.bindBankCard();">绑定</button> ';
	htmlStr += '<button class="btn btn-info" type="button" onClick=$ddf_bank.showDiv("content0")>返回</button>  ';
	htmlStr += '</div>  ';
	htmlStr += '</form>';
	htmlStr += '</div>';
	htmlStr += '</div>  ';
	
	
	htmlStr += '</div>  ';
	htmlStr += '</div>';
	htmlStr += '</div> ';
	htmlStr += '</div> ';

	showDiv("content1");

	$("#content1").html(htmlStr);
	
	setRadioStyle();
	
	
	$("#input-bankNumber").change(function() {
		var bankNumber=$("#input-bankNumber").val();

		if(isNull(bankNumber)){
			addErrorMessage("control-group-bankNumber","input-bankNumber","银行卡号非法");
		}else{
			//if(accountTypeId=="00"){
				if(!isBankNumber(bankNumber)){
					addErrorMessage("control-group-bankNumber","input-bankNumber","卡号非法,必须为16-20位数字");
				}else{
					clearErrorMessage("control-group-bankNumber");
				}
			//}else{
			//	clearErrorMessage("control-group-bankNumber");
			//}
			
		}

	});
	
	$("#input-accountName").change(function() {

		var accountName=$("#input-accountName").val();

		if(isNull(accountName)){
			addErrorMessage("control-group-accountName","input-accountName","持卡人姓名非法");
		}else{
			clearErrorMessage("control-group-accountName");
		}

	});
	
	/*
	$("#input-identity").change(function() {
		
		var identity=$("#input-identity").val();
		if(isNull(identity)){
			addErrorMessage("control-group-identity","input-identity","证件号码不能为空");
		}else if(isLength(identity,50)){
			addErrorMessage("control-group-identity","input-identity","证件号码长度不能大于50字节");
		}else{
			if(identifyTypeId==1){
				if(!isIdCard(identity)){
					addErrorMessage("control-group-identity","input-identity","身份证号非法");
				}else{
					clearErrorMessage("control-group-identity");
				}
			}else if(identifyTypeId==2){
				if(!isJgCard(identity)){
					addErrorMessage("control-group-identity","input-identity","军人证号非法");
				}else{
					clearErrorMessage("control-group-identity");
				}
			}else{
				clearErrorMessage("control-group-identity");
			}
		}
		
	});
	
	$("#input-mobile").change(function() {
		
		var mobile=$("#input-mobile").val();
		
		if(!isMobile(mobile)){
			addErrorMessage("control-group-mobile","input-mobile","手机号码非法");
		}else{
			clearErrorMessage("control-group-mobile");
		}
		
	});
	*/
}



/**
 * 绑定银行卡
 */
function bindBankCard(){
	var bankNumber=$("#input-bankNumber").val();
	var accountName=$("#input-accountName").val();
	//var identity=$("#input-identity").val();
	//var mobile=$("#input-mobile").val();
//	alert("bankId = " + bankId);
//	
////	alert("bankId = " + bankId);
	if(bankId=="-1" || bankId==""){
		addErrorMessage("control-group-bankId","input-bankCard","请选择银行");
		return;
	}
	
	if(isNull(bankNumber)){
		addErrorMessage("control-group-bankNumber","input-bankNumber","银行卡号非法");
		return;
	}else /*if(accountTypeId=="00")*/{
		if(!isBankNumber(bankNumber)){
			addErrorMessage("control-group-bankNumber","input-bankNumber","卡号非法,必须为16-20位数字");
			return;
		}
	}
	
	
	if(isNull(accountName)){
		addErrorMessage("control-group-accountName","input-accountName","持卡人姓名非法");
		return;
	}
	/*
	if(isNull(identity)){
		addErrorMessage("control-group-identity","input-identity","证件号码不能为空");
		return;
	}else if(isLength(identity,50)){
		addErrorMessage("control-group-identity","input-identity","证件号码长度不能大于50字节");
		return;
	}else{
		if(identifyTypeId==1){
			if(!isIdCard(identity)){
				addErrorMessage("control-group-identity","input-identity","身份证号非法");
				return;
			}else{
				clearErrorMessage("control-group-identity");
			}
		}else if(identifyTypeId==2){
			if(!isJgCard(identity)){
				addErrorMessage("control-group-identity","input-identity","军人证号非法");
				return;
			}else{
				clearErrorMessage("control-group-identity");
			}
		}else{
			clearErrorMessage("control-group-identity");
		}
	}
	
	
	if(!isMobile(mobile)){
		addErrorMessage("control-group-mobile","input-mobile","手机号码非法");
		return;
	}
	
	*/
	var params={
			
			"sgAccounts.accountName":accountName,
			"sgAccounts.bankId":bankId,
			"sgAccounts.bankNumber":bankNumber,
			//"sgAccounts.identifyTypeId":identifyTypeId,
			//"sgAccounts.identity":identity,
			//"sgAccounts.mobile":mobile,
			//"sgAccounts.accountType":accountTypeId,
			"defaultaccount":defaultAcount
			
	        };
	
	
	base.showLoading();
	
	eval(Wind.compile("async",function(){
		
		var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.bank.bindBankCard,params));
		
		bindBankCardCallBack(data);
		
	}))().start();
	
}

/**
 * 绑定银行卡_回调函数
 * @param data
 */
function bindBankCardCallBack(data){
	if(data["code"]==1){
		showDiv("content0");
		getQueryBankList(1,pageSize);
		base.tips("银行卡绑定成功！");
	}else{
		 base.tips_error(data["message"]);
	}
	
	base.cancelLoading();
}


/**
 * 选择证件类型
 */
var identifyTypeId="1";

function setCardType(type,name){
	
	
	identifyTypeId=type;
	
	$("#card_type_name").html(name);
	
}

/**
 * 选择账户类型
 */
var accountTypeId="00";

function setAccountType(id,name){
	
	accountTypeId=id;
	
	$("#account_type_name").html(name);
	
}



/**
 * 选择银行
 */
var bankId="-1";
function setBank(_bankId){
	
	bankId=_bankId;
	
}


/**
 * 设置是否选择默认
 */
var defaultAcount="true";
function setDefaultAcount() {
	if ($("#defaultAcountCB").is(":checked")) {
		defaultAcount="true";
	}else{
		defaultAcount="false";
	}
}

/**
 * 是否已读条款
 */
function redisabled() {
	if ($("#redisabledCB").is(":checked")) {
		$("#btnBindBankCard").attr("disabled", false);
	} else {
		$("#btnBindBankCard").attr("disabled", true);
	}
}


/**
 * 打开银行列表
 */
exports.openBankCardDialog=function(){

	dialog.ddf_dialog_bank($("#input-bankCard"),function(id,name){  
		
		$("#input-bankCard").val(name);
		
		setBank(id);

		
    });
	
};





/**
 * 显示DIV
 */
function showDiv(block){
	
	$("#content > div").hide();
	
	if($("#"+block).length<=0){
		
		$("#content").append('<div id="'+block+'"></div>'); 
		
	}else{
		
		$("#"+block).show();
	}
	
	if(block=="content0"){
		shopTypeId="";
	}
	

	
}


//暴露方法
exports.getQueryBankList=getQueryBankList;
//exports.unbindbanks=unbindbanks;
//exports.rebindbanks=rebindbanks;
exports.setdefaultaccount=setdefaultaccount;
exports.bindingbankcardPage=bindingbankcardPage;
exports.bindBankCard=bindBankCard;
exports.setCardType=setCardType;
exports.setAccountType=setAccountType;
exports.setDefaultAcount=setDefaultAcount;
exports.redisabled=redisabled;
exports.showDiv=showDiv;

});
